package home_8_25.view.impl;

import home_8_25.dao.AdminDao;
import home_8_25.dao.BusinessDao;
import home_8_25.dao.impl.AdminDaoImpl;
import home_8_25.dao.impl.BusinessDaoImpl;
import home_8_25.view.UserView;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

import static home_8_25.util.ReadText.*;
import static home_8_25.util.ReadText.getIn;

public class AdminView extends UserView {
    private BusinessDao businessDao = new BusinessDaoImpl();
    private AdminDao adminDao = new AdminDaoImpl();
    @Override
    public Map<String, Object> login() {


        Map<String,Object> map = null;
        Scanner in = new Scanner(System.in);
        System.out.println("管理员账号");
        String name = in.next();
        System.out.println("管理员密码");
        String pwd = in.next();
        map = adminDao.login(name,pwd);


        return map;
    }

    @Override
    public void menu() {
        System.out.println("管理员功能如下");
        System.out.println("1.查询商家");
        System.out.println("2.添加商家");
        System.out.println("3.删除商家");
        System.out.println("4.修改商家");
        int n = getIn().nextInt();

        switch (n){
            case 1:
                searchBusinessMenu();
                break;
            case 2:
                addBusiness();
                break;

            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            default:
                System.out.println("请输入正确指令");
        }

    }

    private void addBusiness() {
        System.out.println("请输入商家的信息");
        System.out.println("商家的名称：");
        String name = getIn().next();
        System.out.println("商家的ID：");
        String bid = getIn().next();
        System.out.println("商家的地址：");
        String address = getIn().next();
        System.out.println("商家的介绍");
        String explian = getIn().next();

        int row = businessDao.addBusiness(name,bid,address,explian);
        if (row>0){
            System.out.println("成功");
        }else
        {
            System.out.println("失败");
        }
    }


    private void searchBusinessMenu() {

        System.out.println("1.查询所有商家");
        System.out.println("2.按照功能查询");
        System.out.println("3.查询商家和厂品数量");
        System.out.println("请选择功能");
        int n = getIn().nextInt();
        switch (n){
            case 1:
                searchBusinessAll();
                break;
            case 2:
                searchBusinessByName();
                break;
            case 3:
                searchBusFood();
                break;
            default:
                System.out.println("请输入正确指令");
        }
        System.out.println("");
        System.out.println("");
        System.out.println("");

    }
    private void searchBusFood() {

        List<Map<String, Object>> busfood = businessDao.statistics();
        System.out.println("商家名称\t商品的数量");
        for (int i = 0; i < busfood.size(); i++) {
            Map<String,Object> map = busfood.get(i);

            Object name = map.get("busname");
            Object address = map.get("fnum");

            System.out.println(name+"\t"+address);
        }
    }
    private void searchBusinessAll() {
        List<Map<String,Object>> list = businessDao.finAll();
        System.out.println("*******");
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            Object id = map.get("busid");
            Object name = map.get("busname");
            Object address = map.get("busaddress");
            Object starprice = map.get("bussprice");
            Object deliverprice = map.get("bus_deliveryprice");
            Object status = map.get("business_status");
            System.out.println(id+"\t"+name+"\t"+address+"\t"+starprice+"\t"+deliverprice+"\t"+status);
        }
    }

    private void searchBusinessByName() {
        System.out.println("请输入查询的名称");
        String n = getIn().next();
        List<Map<String,Object>> list = businessDao.findByName(n);
        System.out.println("*******");
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            Object id = map.get("busid");
            Object name = map.get("busname");
            Object address = map.get("busaddress");
            Object starprice = map.get("bussprice");
            Object deliverprice = map.get("bus_deliveryprice");
            Object status = map.get("business_status");
            System.out.println(id+"\t"+name+"\t"+address+"\t"+starprice+"\t"+deliverprice+"\t"+status);
        }
    }

}
